package com.synergy.stockflow.handler;

import com.synergy.stockflow.constant.ResultCode;
import com.synergy.stockflow.exception.BusinessException;
import com.synergy.stockflow.model.Result;
import org.apache.catalina.connector.ClientAbortException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义业务异常
     */
    @ExceptionHandler(BusinessException.class)
    public Result<?> handleBusinessException(BusinessException e) {
        return Result.error(e.getCode(), e.getMessage(), e.getData());
    }


    /**
     * 处理其他所有未被捕获的异常
     */
    @ExceptionHandler(Exception.class)
    public Result<?> handleOtherException(Exception e) {
        e.printStackTrace();
        return Result.error(ResultCode.SYSTEM_EXCEPTION.value(), "系统内部异常");
    }
    @ExceptionHandler(ClientAbortException.class)
    public Result<String> handleClientAbort() {
        return Result.error(499, "Client closed request");
    }
}